package com.xydtech.rule.controller;

import java.util.ArrayList;
import java.util.List;

import com.xydtech.rule.domain.RuleElementsGroup;
import com.xydtech.rule.domain.RuleElementsItems;
import com.xydtech.rule.service.IRuleElementsGroupService;
import com.xydtech.rule.service.IRuleElementsItemsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xydtech.common.annotation.Log;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.rule.domain.RuleElementsGroupDetail;
import com.xydtech.rule.service.IRuleElementsGroupDetailService;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.common.core.page.TableDataInfo;

/**
 * 规则组明细Controller
 *
 * @author xydtech
 * @date 2024-06-07
 */
@Controller
@RequestMapping("/rule/groupDetail")
public class RuleElementsGroupDetailController extends BaseController {
    private String prefix = "rule/groupDetail";
    @Autowired
    private IRuleElementsGroupDetailService ruleElementsGroupDetailService;

    @RequiresPermissions("rule:group:view")
    @GetMapping()
    public String groupDetail() {
        return prefix + "/groupDetail";
    }

    @GetMapping("/{groupId}")
    public String groupList(@PathVariable("groupId") Long groupId, ModelMap mmap) {
        mmap.put("groupId", groupId);
        return prefix + "/itemsList";
    }


    /**
     * 查询规则组明细列表
     */
    @RequiresPermissions("rule:group:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RuleElementsGroupDetail ruleElementsGroupDetail) {
        startPage();
        List<RuleElementsGroupDetail> list = ruleElementsGroupDetailService.selectRuleElementsGroupDetailList(ruleElementsGroupDetail);
        return getDataTable(list);
    }


    /**
     * 新增规则组明细
     */
    @GetMapping("/add/{groupId}")
    public String add(@PathVariable("groupId") Long groupId, ModelMap mmap) {
        mmap.put("groupId", groupId);
        return prefix + "/elementList";
    }

    /**
     * 新增保存规则组明细
     */
    @RequiresPermissions("rule:group:add")
    @Log(title = "新增保存规则组明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Long groupId, String itemsIds) {
        List<RuleElementsGroupDetail> detailsList = new ArrayList<>();
        String[] itemsId = itemsIds.split(",");
        for (int i = 0; i < itemsId.length; i++) {
            RuleElementsGroupDetail groupDetail=new RuleElementsGroupDetail();
            groupDetail.setGroupId(groupId);
            groupDetail.setItemsId(Long.valueOf(itemsId[i]));
            detailsList.add(groupDetail);
        }
        return toAjax(ruleElementsGroupDetailService.batchInsertRuleElementsGroupDetail(detailsList));
    }

    /**
     * 修改保存规则组明细
     */
    @RequiresPermissions("rule:group:edit")
    @Log(title = "规则组明细", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RuleElementsGroupDetail ruleElementsGroupDetail) {
        return toAjax(ruleElementsGroupDetailService.updateRuleElementsGroupDetail(ruleElementsGroupDetail));
    }

    /**
     * 删除规则组明细
     */
    @RequiresPermissions("rule:group:remove")
    @Log(title = "规则组明细", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ruleElementsGroupDetailService.deleteRuleElementsGroupDetailByIds(ids));
    }
}
